<script>
import Vue from 'vue'
import { mapState, mapActions } from 'vuex'
export default {
	onLaunch() {
		// #ifdef APP-PLUS
		// 锁定屏幕方向
		plus.screen.lockOrientation('portrait-primary') //锁定
		// #endif
		uni.getSystemInfo({
			success(res) {
				Vue.prototype.safeArea = res.safeArea
				// #ifndef MP
				Vue.prototype.StatusBar = res.statusBarHeight
				Vue.prototype.CustomBar = res.statusBarHeight + 45
				// #endif

				// #ifdef MP-WEIXIN || MP-QQ
				Vue.prototype.StatusBar = res.statusBarHeight
				let capsule = wx.getMenuButtonBoundingClientRect()
				if (capsule) {
					Vue.prototype.Custom = capsule
					Vue.prototype.CustomBar = capsule.bottom + capsule.top - res.statusBarHeight
				} else {
					Vue.prototype.CustomBar = res.statusBarHeight + 50
				}
				// #endif

				// #ifdef MP-ALIPAY
				Vue.prototype.StatusBar = res.statusBarHeight
				Vue.prototype.CustomBar = res.statusBarHeight + res.titleBarHeight
				// #endif

				let adapterHeight = 0

				let { system, model, screenHeight, screenWidth } = res
				let isIOS = () => {
					return system.indexOf('iOS') != -1
				}
				let isIPhoneX = () => {
					// return isIOS() && model.indexOf('X') != -1
					return isIOS() && screenHeight / screenWidth > 1.78
				}

				if (isIPhoneX()) {
					adapterHeight = 34
				}
				Vue.prototype.adapterHeight = adapterHeight
			}
		})
		uni.getStorage({
			key: 'locale',
			success: e => {
				this.$i18n.locale = e.data
			}
		})
	},
	onShow() {
		this.checkUpgrade()
	},
	methods: {
		...mapActions(['setUserlnt','setUserlat','setLive']),
		getaddress(){
			var _this= this;
			uni.getLocation({
			    type: 'wgs84',
				geocode:true,
			    success: function (res) {
					_this.setUserlnt(res.longitude)
					_this.setUserlat(res.latitude)
					_this.setLive(res.address.city)
			    }
			});
		},
		async checkUpgrade() {
			// 检测升级
			let params = {
				// // #ifdef APP-PLUS
				// appid: plus.runtime.appid,
				// num: this.version,
				// type: plus.os.name === 'iOS' ? 2 : 1
				// // #endif
			}
			
			let {
				data
			} = await this.$http.post('login.upgrade', params, { isAuth: false, tipConfig: { isLoading: false, isErrorDefaultTip: false } })
			// #ifdef H5
			let Android=data[0].downloadurl
			let iOS=data[1].downloadurl
			this.setState({ key: 'downloadURL', value: { Android, iOS } })
			// #endif
			// #ifdef APP-PLUS
			if(plus.os.name=='iOS'){
				let newversion=data[1].newversion
				if(this.version!=newversion){
					this.setState({ key: 'isUpgrade', value: this.version!=newversion })
					let res = await this.$uni.showModal({ title: this.i18n.updateTip, showCancel: true, content: data[1].content })
					res.confirm && plus.runtime.openURL(data[1].downloadurl)
					res.cancel && (this.version!=newversion ? plus.runtime.quit() : null)
				}
			}else{
				let newversion=data[0].newversion
				if(this.version!=newversion){
					this.setState({ key: 'isUpgrade', value: this.version!=newversion })
					let res = await this.$uni.showModal({ title: this.i18n.updateTip, showCancel: true, content: data[0].content })
					res.confirm && plus.runtime.openURL(data[0].downloadurl)
					res.cancel && (this.version!=newversion ? plus.runtime.quit() : null)
				}
			}
			// #endif
			
		}
	}
}
</script>

<style lang="less">
/*每个页面公共css */
@import './style/bg.css';
@import './style/color.less';
@import './style/colorui/main.css';
@import './style/colorui/icon.css';
@import './style/base.less';
@import './style/paybox.less';
@import './style/yidangdang.less';
</style>
